/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cw.biz;

import cw.controller.QuestionJpaController;
import cw.entity.Puzzle;
import cw.entity.Question;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author VinhTC
 */
public class QuestionBiz {

    QuestionJpaController qjc = new QuestionJpaController();
    PuzzleBiz puzzbiz = new PuzzleBiz();
    public QuestionBiz() {
    }

    /**
     * addQuestion
     * @param ques
     * @return boolean
     */
    public boolean addQuestion(List<Question> quesList, Puzzle puzzle) {
        boolean result = false;
        try {
            for (int i= 0; i < quesList.size(); i++) {
                qjc.create(quesList.get(i));
            }
            if (puzzbiz.updatePuzzle(puzzle)) {
                result = true;
            }
        } catch (Exception e) {
            Logger.getLogger(QuestionBiz.class.getName()).log(Level.SEVERE, null, e);
        }
        return result;
    }

    /**
     * getQuestionByPuzzleId
     * @param puzzleId
     * @return List<Question>
     */
    public List<Question> getQuestionByPuzzleId(int puzzleId){
        List<Question> lst = new ArrayList<Question>();
        lst = qjc.getQuestionByPuzzleId(puzzleId);
        return lst;
    }

    /**
     * getQuestionByQuestionContent
     * @param quest
     * @return Question
     */
    public Question getQuestionByQuestionContent(String quest){
        return qjc.getQuestionByQuestionContent(quest);
    }

    /**
     * getQuestionBySubjectId
     * @param subjectId
     * @return List<Question>
     */
    public List<Question> getQuestionBySubjectId(int subjectId){
        List<Question> lst = new ArrayList<Question>();
        List<Question> quests = new ArrayList<Question>();
        List<Puzzle> puzzles = new ArrayList<Puzzle>();
        PuzzleBiz puzzBiz = new PuzzleBiz();

        puzzles = puzzBiz.getPuzzleBySubjectId(subjectId);

        for (int i= 0; i < puzzles.size(); i++) {
            lst = qjc.getQuestionByPuzzleId(puzzles.get(i).getPuzzleId());
            quests.addAll(lst);
        }
        
        return quests;
    }

    /**
     * updateQuestion
     * @param ques
     * @return boolean
     */
    public boolean updateQuestion(Question ques){
        boolean result =false;
        try {
            qjc.edit(ques);
            result = true;
        } catch (Exception e) {
        }
        return result;
    }
}
